package com.ht.medicaltreatment.viewmodel

import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.ToastUtils
import com.ht.medicaltreatment.bean.WanDaiBean
import com.xgst.driverlesscars.api.ApiResponse
import com.xgst.lib.http.ApiResult
import kotlinx.coroutines.launch

class SingleViewModel : BaseViewModel() {

    //紧急跳过
    fun caixueJump(id:String,onSuccess:()->Unit){
        viewModelScope.launch {
            val bean =  mApi.caixueJump(id)
            when(bean){
                is ApiResult.Success -> onSuccess()
                is ApiResult.Error -> ToastUtils.showShort(bean.errorMessage)
            }
        }
    }

    //扫描腕带id
    fun wanDai(id:String,isOrder:Boolean = false,onSuccess:(WanDaiBean)->Unit,onError: (String) -> Unit = {}){
        viewModelScope.launch {
            var bean: ApiResult<ApiResponse<WanDaiBean>>
            if(isOrder){
                 bean =  mApi.caixueDetail(id)
            }else{
                 bean =  mApi.wanDai(id,"301医院","PATIENTCHECK")
            }
            when(bean){
                is ApiResult.Success -> onSuccess(bean.data.data!!)
                is ApiResult.Error -> onError(bean.errorMessage)
            }
        }
    }

    //根据采样id查看采样样本
    fun caiyang(id:String,onSuccess:()->Unit){
        viewModelScope.launch {
            val bean =  mApi.caiyang(id)
            when(bean){
                is ApiResult.Success -> onSuccess()
                is ApiResult.Error -> ToastUtils.showShort(bean.errorMessage)
            }
        }
    }

    /**
     * 查询uid对应的名字
     */
    fun searchName(uid:MutableList<String>, onResult:(MutableList<String>)->Unit, onError:(String)->Unit){
        viewModelScope.launch {
            when(val bean = mApi.searchNameScan(uid)){
                is ApiResult.Success -> {
                    val sampleInfoList = bean.data.data?.sampleInfoList
                    if(sampleInfoList != null && sampleInfoList.size > 0){
                        val list = arrayListOf<String>()
                        sampleInfoList.forEach {
                            list += it.name
                        }
                        onResult(list)
                    }else{
                        onError("无数据")
                    }
                }
                is ApiResult.Error -> onError(bean.errorMessage)
            }
        }
    }

    /**
     * 查询uid对应的名字
     */
    fun searchNameByBarCode(uid:MutableList<String>, onResult:(MutableList<String>)->Unit, onError:(String)->Unit){
        viewModelScope.launch {
            when(val bean = mApi.searchNameByBarCode(uid)){
                is ApiResult.Success -> {
                    val sampleInfoList = bean.data.data?.sampleInfoList
                    if(sampleInfoList != null && sampleInfoList.size > 0){
                        val list = arrayListOf<String>()
                        sampleInfoList.forEach {
                            list += it.name
                        }
                        onResult(list)
                    }else{
                        onError("无数据")
                    }
                }
                is ApiResult.Error -> onError(bean.errorMessage)
            }
        }
    }

    //记录采血时间
    fun bloodCollectionTime(time:String,drawBloodId:String,onSuccess: () -> Unit){
        viewModelScope.launch {
            when(val bean = mApi.bloodCollectionTime(drawBloodId,time)){
                is ApiResult.Success -> onSuccess()
                is ApiResult.Error -> ToastUtils.showShort(bean.errorMessage)
            }
        }
    }

    //添加备注信息
    fun addRemarks(collBloodTime:String,drawBloodId:String,otherRemarks:String,onSuccess: () -> Unit){
        viewModelScope.launch {
            when(val bean = mApi.addRemarks(drawBloodId,otherRemarks,collBloodTime)){
                is ApiResult.Success -> onSuccess()
                is ApiResult.Error -> ToastUtils.showShort(bean.errorMessage)
            }
        }
    }

    //添加备注信息
    fun updateDrawblood(id:String,otherRemarks:String,onSuccess: () -> Unit){
        viewModelScope.launch {
            when(val bean = mApi.updateDrawblood(id,otherRemarks)){
                is ApiResult.Success -> onSuccess()
                is ApiResult.Error -> ToastUtils.showShort(bean.errorMessage)
            }
        }
    }

    //记录核验时间
    fun recordCheckTime(drawBloodIdList:String,onSuccess: () -> Unit){
        viewModelScope.launch {
            when(val bean = mApi.recordCheckTime(drawBloodIdList)){
                is ApiResult.Success -> onSuccess()
                is ApiResult.Error -> ToastUtils.showShort(bean.errorMessage)
            }
        }
    }

    //替换
    fun replace(drawBloodId:String,replaceRfidCode:String,onSuccess: () -> Unit,onError: (String) -> Unit){
        viewModelScope.launch {
            when(val bean = mApi.bloodReplace(drawBloodId,replaceRfidCode)){
                is ApiResult.Success -> onSuccess()
                is ApiResult.Error -> onError(bean.errorMessage)
            }
        }
    }
}
